package JDBC;

import com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBC2 {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java112?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("wlx020701");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3.构建 SQL
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行 SQL ,这一步和前面就不一样了
        ResultSet resultSet = statement.executeQuery();

        //5.遍历结果循环
        while(resultSet.next()){
            //循环里针对一行进行处理
            System.out.println(resultSet.getInt("id"));
            System.out.println(resultSet.getString("name"));
        }

        //6.关闭对应的资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
